<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>返利开票申请表-针对支付表 管理</title>
    <jsp:include page="../../import.jsp" flush="true"/>
</head>
<body class="iframeBody">
<div class="Wrapper">
    <div class="searchArea">
        <form id="prjRbtbillApply_form">
            <div class="fieldset1 clearFix">
                <div class="clearFix"></div>
                <input name="applyId" id="applyId" type="text" style="display: none;" value="${applyId}">
                <div class="btnsBox">
                    <c:if test="${not empty applyId}">
                        <span id="modify_Btn" class="sq-button sq-button-round">保存</span>
                    </c:if>
                    <c:if test="${empty applyId}">
                        <span id="save_Btn" class="sq-button sq-button-round">发起申请</span>
                    </c:if>
                </div>

                <div class="fieldset1 tabel_col clearFix">
                    <div class="fieldBox col_33">
                        <div class="label">返利金额合计</div>
                        <div class="field">
                            <input name="rbtAmt" id="rbtAmt" type="text" disabled="disabled" class="ui-textbox" value="${rbtAmts}" validate="{required:true,number:true}">
                            <input name="rbtAmount" id="rbtAmount" type="text" style="display: none;" value="${rbtAmount}">
                        </div>
                    </div>
                    <div class="fieldBox col_33">
                        <div class="label">开票金额</div>
                        <div class="field">
                            <input name="billAmt" id="billAmt" type="text" disabled="disabled" class="ui-textbox" value="${billAmts}"  validate="{required:true,number:true}">
                        </div>
                    </div>
                    <div class="fieldBox col_33">
                        <div class="label">开票税点</div>
                        <div class="field">
                            <input name="billTpAmt" id="billTpAmt" type="text" disabled="disabled" class="ui-textbox" value="${billTpAmts}"  validate="{required:true,number:true}">
                            <input name="billTpPct" id="billTpPct" type="hidden" class="ui-textbox" value="1.06">
                            <input name="applyListJson" id="applyListJson" type="hidden" value="">
                            <!-- 用于存放申请列表数据 -->
                        </div>
                    </div>
                    <div class="clearFix"></div>

                    <div class="fieldBox col_33">
                        <div class="label">账户名</div>
                        <div class="field">
                            <%--<input name="bkid" id="bkid" type="hidden" class="ui-textbox" value="${bkid}">--%>
                            <input name="rcptUnt" id="rcptUnt"  type="text" class="ui-textbox" value="${rcptUnt }" validate="{required:true, maxlength:20}">
                            <span class="requiredSymbol">*</span>
                        </div>
                    </div>
                    <div class="fieldBox col_33">
                        <div class="label">开户行及支行</div>
                        <div class="field">
                            <input name="bkNm" id="bkNm" type="text" class="ui-textbox" value="${bkNm }" validate="{required:true, maxlength:60}">
                            <span class="requiredSymbol">*</span>
                        </div>
                    </div>

                    <div class="fieldBox col_33">
                        <div class="label">开户行账号</div>
                        <div class="field">
                            <input name="accNo" id="accNo" type="text" class="ui-textbox" value="${accNo }" validate="{required:true,number:true, maxlength:60}">
                            <span class="requiredSymbol">*</span>
                        </div>
                    </div>

                    <div class="fieldBox col_33">
                        <div class="label">开户行行号</div>
                        <div class="field">
                            <input name="bkNo" id="bkNo" type="text" class="ui-textbox" value="${bkNo }" validate="{required:true,number:true, maxlength:30}">
                            <span class="requiredSymbol">*</span>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>

</div>
<div id="ligergrid1" class="sq-grid"></div>


</div>
<script>
    var form = null;
    var grid = null;
    $(function () {
        grid = $("#ligergrid1").ligerGrid({
            url: '<%=path%>/project/prjRbtbillBscInfo/getApplyListPage.do?applyId=${applyId}',
            method: 'get',
            columns: [
                {display: '支付表号', name: 'paySchNo', minWidth: 100, align: 'left', enabledSort: false},
                {
                    display: '供应商返利金额', name: 'splRbtAmt', minWidth: 100, align: 'right',
                    render: function (rowdata, index, value) {
                        return '¥' + FormatMoney(rowdata.splRbtAmt / 100);
                    }, enabledSort: false
                },
                {
                    display: 'GPS返利金额', name: 'gpsRbtAmt', minWidth: 100, align: 'right',
                    render: function (rowdata, index, value) {
                        return '¥' + FormatMoney(rowdata.gpsRbtAmt / 100);
                    }, enabledSort: false
                },
                {
                    display: '销售返利金额', name: 'saleRbtAmt', minWidth: 100, align: 'right',
                    render: function (rowdata, index, value) {
                        return '¥' + FormatMoney(rowdata.saleRbtAmt / 100);
                    }, enabledSort: false
                },
                {
                    display: '供应商阶梯返利金额', name: 'stepRebateAmt', minWidth: 100, align: 'right',
                    render: function (rowdata, index, value) {
                        return '¥' + FormatMoney(rowdata.stepRebateAmt / 100);
                    }, enabledSort: false
                },
                {display: '狮桥返利', name: '', minWidth: 100, align: 'left', enabledSort: false},
                {display: '起租日', name: 'lsDt', minWidth: 100, align: 'left', render: renderDate, enabledSort: false},
                {display: '项目类型', name: 'prjTypNm', minWidth: 100, align: 'left', enabledSort: false},
                {display: '资管机构', name: 'fundRasInsNm', minWidth: 100, align: 'left', enabledSort: false},
                {display: '所属事业部', name: 'buOrgNm', minWidth: 100, align: 'left', enabledSort: false},
            ],
            pageSize: 10,
            root: 'rows',
            record: 'total',
            hideLoadButton: true,
            width: '100%',
            height: '100%',
            heightDiff: -2,
            checkbox: true,
            rownumbers: true,
            allowUnSelectRow: false,
            alternatingRow: false,
            onSelectRow: function (rowdata, rowindex) {
                var selectedRows = grid.getSelectedRows();
                if (selectedRows.length > 0) {
                    var rbtAmt = 0;
                    for(var i = 0; i < selectedRows.length; i++){
                        rbtAmt += selectedRows[i].splRbtAmt + selectedRows[i].gpsRbtAmt + selectedRows[i].saleRbtAmt + selectedRows[i].stepRebateAmt;
                    }
                    console.log("onSelectRow.rbtAmt = "+rbtAmt);
                    $("#rbtAmt").val(FormatMoney(rbtAmt / 100));
                    $("#billTpAmt").val(FormatMoney(rbtAmt/0.94*0.06 / 100));
                    $("#billAmt").val(FormatMoney(rbtAmt/100+rbtAmt/0.94*0.06 / 100));
                    $("#rbtAmount").val(rbtAmt);
                }
            },
            onUnSelectRow: function (rowdata, rowindex) {
                var selectedRows = grid.getSelectedRows();
                if (selectedRows.length > 0) {
                    var rbtAmt = 0;
                    for(var i=0;i<selectedRows.length;i++){
                        rbtAmt += selectedRows[i].splRbtAmt + selectedRows[i].gpsRbtAmt + selectedRows[i].saleRbtAmt + selectedRows[i].stepRebateAmt;
                    }
                    console.log("onUnSelectRow.rbtAmt = "+rbtAmt);
                    $("#rbtAmt").val(FormatMoney(rbtAmt / 100));
                    $("#billTpAmt").val(FormatMoney(rbtAmt/0.94*0.06 / 100));
                    $("#billAmt").val(FormatMoney(rbtAmt/100+rbtAmt/0.94*0.06 / 100));
                    $("#rbtAmount").val(rbtAmt);
                } else {
                    $("#rbtAmt").val(FormatMoney(0));
                    $("#billTpAmt").val(FormatMoney(0));
                    $("#billAmt").val(FormatMoney(0));
                    $("#rbtAmount").val(rbtAmt);
                }
            },
            isChecked:function(item){
                if(1==item.checkFlag){
                    return true;
                }
            },
        });
        grid.setHeight(100);
        $("#pageloading").hide();

        form = $("#prjRbtbillApply_form").ligerForm({
            validate: true
        });

        /*保存并发起流程*/
        $("#save_Btn").click(function (event) {
            //var form = liger.get("prjRbtbillApply_form");
            if (!form.valid()) {
                $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_MUST_FIX_ITEM_INFO, SQ_type: "warning"});
                return false; //验证不通过，不执行以下代码
            }
            var selectedRows = grid.getSelectedRows();
            if (selectedRows.length == 0) {
                $.ligerDialog.SQalert({content: "请先选择要发起报销流程的支付表", SQ_type: "warning"});
                return;
            }
            var buOrgCd = "";
            var applyListJson = [];
            for (var index in selectedRows) {
                if(!buOrgCd){
                    buOrgCd = selectedRows[index].buOrgCd;
                }
                if(buOrgCd != selectedRows[index].buOrgCd){
                    $.ligerDialog.SQalert({content: "请选择同一个事业部支付表进行报销", SQ_type: "warning"});
                    applyListJson = [];
                    return;
                }
                applyListJson.push({'paySchId':selectedRows[index].paySchId,'paySchNo':selectedRows[index].paySchNo,'splRbtAmt':selectedRows[index].splRbtAmt,
                    'gpsRbtAmt':selectedRows[index].gpsRbtAmt,'saleRbtAmt':selectedRows[index].saleRbtAmt,'lsDt':selectedRows[index].lsDt});
            }

            $.ligerDialog.confirm('您确定要发起返利报销流程吗？', function (confirm) {
                if (confirm) {
                    var data = form.getData();
                    data.billAmt = $('#billAmt').val().replace(",","");
                    data.rbtAmt = $('#rbtAmt').val().replace(",","");
                    data.billTpAmt = $('#billTpAmt').val().replace(",","");
                    data.billTpPct = $('#billTpPct').val().replace(",","");
                    data.id = $('#applyId').val();
                    data.buOrgCd = buOrgCd;
                    data.applyListJson = JSON.stringify(applyListJson);
                    var _url = '<%=path%>/project/prjRbtbillApply/add.do';
                    var wait_dialog;
                    $.ajax({
                        type: 'post',
                        dataType: 'json',
                        data: data,
                        url: _url,
                        beforeSend: function () {
                            wait_dialog = $.ligerDialog.SQalert({
                                content: LogUtils.LOG_CMM_DATA_LOADING,
                                SQ_type: "waitting"
                            });
                        },
                        success: function (msg) {
                            wait_dialog.close();
                            if (msg.success == 1) {
                                $.ligerDialog.SQalert({content: msg.info, SQ_type: "success"});
                                //setInterval('Remove_navTab();callback();', 1000);
                                //回调刷新列表
                                var callback = GET_W2Wcallback();
                                if(callback){
                                    callback();
                                }
                                Remove_navTab();
                            } else {
                                $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                            }
                        }
                    });
                }
            });
        });
        /*保存修改*/
        $("#modify_Btn").click(function (event) {
            if (!form.valid()) {
                $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_MUST_FIX_ITEM_INFO, SQ_type: "warning"});
                return false; //验证不通过，不执行以下代码
            }
            var selectedRows = grid.getSelectedRows();
            if (selectedRows.length == 0) {
                $.ligerDialog.SQalert({content: "请先选择要报销的支付表", SQ_type: "warning"});
                return;
            }
            var buOrgCd = "";
            var applyListJson = [];
            for (var index in selectedRows) {
                if(!buOrgCd){
                    buOrgCd = selectedRows[index].buOrgCd;
                }
                if(buOrgCd != selectedRows[index].buOrgCd){
                    $.ligerDialog.SQalert({content: "请选择同一个事业部支付表进行报销", SQ_type: "warning"});
                    applyListJson = [];
                    return;
                }
                applyListJson.push({'paySchId':selectedRows[index].paySchId,'paySchNo':selectedRows[index].paySchNo, 'splRbtAmt':selectedRows[index].splRbtAmt,
                    'gpsRbtAmt':selectedRows[index].gpsRbtAmt,'saleRbtAmt':selectedRows[index].saleRbtAmt,'lsDt':selectedRows[index].lsDt});
            }

            $.ligerDialog.confirm('您确定要修改报销信息吗？', function (confirm) {
                if (confirm) {
                    var data = form.getData();
                    data.billAmt = $('#billAmt').val().replace(",","");
                    data.rbtAmt = $('#rbtAmt').val().replace(",","");
                    data.billTpAmt = $('#billTpAmt').val().replace(",","");
                    data.billTpPct = $('#billTpPct').val().replace(",","");
                    data.id = $('#applyId').val();
                    data.buOrgCd = buOrgCd;
                    data.applyListJson = JSON.stringify(applyListJson);
                    var _url = '<%=path%>/project/prjRbtbillApply/modify.do';
                    var wait_dialog;
                    $.ajax({
                        type: 'post',
                        dataType: 'json',
                        data: data,
                        url: _url,
                        beforeSend: function () {
                            wait_dialog = $.ligerDialog.SQalert({
                                content: LogUtils.LOG_CMM_DATA_LOADING,
                                SQ_type: "waitting"
                            });
                        },
                        success: function (msg) {
                            wait_dialog.close();
                            if (msg.success == 1) {
                                $.ligerDialog.SQalert({content: msg.info, SQ_type: "success"});
                                //setInterval('Remove_navTab();callback();', 1000);
                            } else {
                                $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                            }
                        }
                    });
                }
            });
        });

        if ("${init}" == "1") {
            //初始化申请列表
            initApplyData();
        }

        /* 查询 */
        $("#searchBtn").click(function (event) {
            var form = liger.get("prjRbtbillApply_form");
            var data = form.getData();
            for (name in data) {
                grid.setParm(name, data[name]);
            }
            grid.reload();
            initApplyData();
        });
        /* 清空 */
        $("#clearBtn").click(function (event) {
            clearLigerForm("prjRbtbillApply_form");
        });

    });

    //格式化时间
    function renderDate(rowdata, index, value) {
        var dateStr = "";
        if (null != value && value != '') {
            var date = new Date(value);
            dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DD);
        }
        return dateStr;
    }

    function initApplyData() {
        var form = liger.get("prjRbtbillApply_form");
        var _url = '<%=path%>/project/prjRbtbillBscInfo/getApplyList.do';
        var data = form.getData();
        //alert(JSON.stringify(data));return;//测试
        $.ajax({
            type: 'get',
            dataType: 'json',
            data: data,
            url: _url,
            success: function (msg) {
                if (msg.success == 1) {
                    $('#applyListJson').val(JSON.stringify(msg.data));//设置数据
                    calAmt(msg.data);
                } else {
                    $.ligerDialog.SQalert({
                        content: msg.info,
                        SQ_type: "error"
                    });
                }
            }
        });
    }
    //计算返利合计，开票金额
    function calAmt(data) {
        var billAmt = 0;//开票金额
        var rbtAmt = 0;//返利金额
        var billTpAmt = 0;//开票税点金额
        var billTpPct = 1.06;
        if (data) {
            console.log("calAmt.data= " + JSON.stringify(data));
            for (var o in data) {
                rbtAmt += (data[o].splRbtAmt + data[o].gpsRbtAmt);
                console.log("计算返利合计，开票金额splRbtAmt:"+data[o].splRbtAmt);
                console.log("计算返利合计，开票金额gpsRbtAmt:"+data[o].gpsRbtAmt);
            }
           // billTpAmt = (billAmt - (rbtAmt / 100)).toFixed(2);
            billTpAmt = (rbtAmt/100/0.94*0.06).toFixed(2);
            $('#billTpAmt').val(billTpAmt);
           // billAmt = ((rbtAmt / 100) * billTpPct).toFixed(2);
            billAmt = (rbtAmt/100+billTpAmt).toFixed(2);
            $('#billAmt').val(billAmt);

            rbtAmt = (rbtAmt / 100).toFixed(2);
            $('#rbtAmt').val(rbtAmt);

        }

    }
</script>
</body>
</html>
